<script setup>

import useUserStore from '@/store/modules/company'
import api from "@/api/setup/company";

const props = defineProps({
  /**当前双向数据绑定的值 */
  value: {
    type: [String, Number],
    default: ''
  },
});

const emit = defineEmits(['update:value', 'update:name']);

const userStore = useUserStore()
const list = ref(userStore.getList());

if (!list.value.length) {
  api.listAll().then((response)=>{
    userStore.setList(response)
    list.value = userStore.getList();
  })
}

function getName(val) {
  const one = list.value.find(item => item.id == val);
  if (one) {
    return one.name;
  }
  return null;
}

const valueId = computed({
  get: () => props.value,
  set: (val) => {
    emit('update:value', val)
    const name = getName(val);
    emit('update:name', name)
  }
});

</script>

<template>
  <el-select v-model="valueId" style="width: 100%;">
    <el-option v-for="item in list" :key="item.id" :label="item.name" :value="item.id"></el-option>
  </el-select>
</template>

<style scoped lang="scss">

</style>